﻿/* $RCSFile: Utilities.asmx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/Services/Utilities.asmx.cs $
 * $log$
 * Revision 20 2010/08/10 17:55:42  christian.surieux
 *   Missing comments in asax asmx ashx files
 * Revision 19 2010/08/09 09:44:01  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;

using AddonNice.Helpers;
using AddonNice.Settings;
using AddonNice.Settings.Cache;
using System.Diagnostics;
using System.Globalization;
using System.Collections;

namespace AddonNice.Services
{
    /// <summary>
    /// Summary description for Utilities
    /// </summary>
    [WebService(Namespace = "http://addonnice.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Utilities : WebService
    {

        [WebMethod]
        public DataTable GetCacheTable(string handshake, bool ShowDetails)
        {
            if ( Config.CacheHandShake != handshake )
                return null;
            return CacheHelper.MakeCacheTb(ShowDetails);
        }

        [WebMethod]
        public bool Remove(string handshake,string key,bool RemoveAll)
        {
           if ( Config.CacheHandShake != handshake )
                return false;
            if ( CacheTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CacheUtil Remove BEG key: {0} RemoveAll: {1}.", key, RemoveAll),CacheTraceSwitch.Sw.Info);
            int count               =   0;
            try
            {
                if ( !RemoveAll )
                    HttpRuntime.Cache.Remove(key);
                else
                {
                    foreach (DictionaryEntry cacheItem in HttpRuntime.Cache)
                    {
                        if ( cacheItem.Key.ToString().StartsWith(key) )
                        {
                            count++;
                            HttpRuntime.Cache.Remove(cacheItem.Key.ToString());
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                if ( CacheTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CacheUtil Remove key: {0} ex: {1}.", key, ex),CacheTraceSwitch.Sw.Info);
                return false;
            }
             if ( CacheTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CacheUtil Remove END key: {0} count: {1}.", key, count),CacheTraceSwitch.Sw.Info);
           return true;
        }

        [WebMethod]
        public bool RemoveCol(string handshake,string[] keys)
        {
           if ( Config.CacheHandShake != handshake )
                return false;
            if ( CacheTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CacheUtil RemoveCol BEG key length: {0} RemoveAll: {1}.", keys.Length),CacheTraceSwitch.Sw.Info);
            int count           =   0;
            try
            {
                foreach ( string key in keys )
                {
                    HttpRuntime.Cache.Remove(key);
                    count++;
                }
            }
            catch (Exception ex)
            {
                if ( CacheTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CacheUtil RemoveCol ex: {0}.", ex),CacheTraceSwitch.Sw.Info);
                return false;
            }
             if ( CacheTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CacheUtil RemoveCol END count: {0}.", count),CacheTraceSwitch.Sw.Info);
            return true;
           }
    }
}
